<?php
class UploadController extends ImageWidgetAppController {

    public function Add() {
        //set whatever data is required to build the page;
        //be sure this is done before instantiating the View class so all
        //set variables are passed when you pass this controller object in the constructor
        $this->layout = false;
        $this->autoRender = false;
        if ($this->request->is('put')) {
            if(isset($_FILES["data"]) && $_FILES["data"]["error"]["Widget"]["image"] == UPLOAD_ERR_OK)
            {
                ############ Edit settings ##############
                $UploadDirectory    = APP.'/Plugin/ImageWidget/webroot/files/'; //specify upload directory ends with / (slash)
                ##########################################

                /*
                Note : You will run into errors or blank page if "memory_limit" or "upload_max_filesize" is set to low in "php.ini".
                Open "php.ini" file, and search for "memory_limit" or "upload_max_filesize" limit
                and set them adequately, also check "post_max_size".
                */

                //check if this is an ajax request
                if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])){
                    die();
                }


                //Is file size is less than allowed size.
                if ($_FILES["data"]["size"]["Widget"]["image"] > 5242880) {
                    die("File size is too big!");
                }

                //allowed file type Server side check
                switch(strtolower($_FILES['data']['type']["Widget"]["image"]))
                {
                    //allowed file types
                    case 'image/png':
                    case 'image/gif':
                    case 'image/jpeg':
                    case 'image/jpg':
                    case 'image/pjpeg':
                    case 'text/plain':
                    case 'text/html': //html file
                    case 'application/x-zip-compressed':
                    case 'application/pdf':
                    case 'application/msword':
                    case 'application/vnd.ms-excel':
                    case 'video/mp4':
                        break;
                    default:
                        die('Unsupported File!'); //output error
                }

                $File_Name          = strtolower($_FILES['data']['name']["Widget"]["image"]);
                $File_Ext           = substr($File_Name, strrpos($File_Name, '.')); //get file extention
                $Random_Number      = rand(0, 9999999999); //Random number to be added to name.
                $NewFileName        = $File_Name; //new file name

                if(move_uploaded_file($_FILES['data']['tmp_name']["Widget"]["image"], $UploadDirectory.$NewFileName ))
                {
                    $this->response->type('json');
                    return json_encode(array('image' => $NewFileName));
                }else{
                    die('error uploading File!');
                }

            }
        }
        else
		    return $this->render('add');
    }
}